package cn.ccew.common.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Map<String ,String> 转json数据，json数据转换为Map
 * 
 * @author lxj
 * 
 */
public class JsonType {
	public static String simpleMapToJsonStr(Map<String, String> map) {
		if (map == null || map.isEmpty()) {
			return "null";
		}
		String jsonStr = "{";
		Set<?> keySet = map.keySet();
		for (Object key : keySet) {
			jsonStr += "\"" + key + "\":\"" + map.get(key) + "\",";
		}
		jsonStr = jsonStr.substring(1, jsonStr.length() - 2);
		jsonStr += "}";
		return jsonStr;
	}

	// {"pass":"4355","name":"12342","wang":"fsf"}
	public Map getData(String str) {
		String sb = str.substring(1, str.length() - 1);
		String[] name = sb.split("\\\",\\\"");
		String[] nn = null;
		Map map = new HashMap();
		for (int i = 0; i < name.length; i++) {
			nn = name[i].split("\\\":\\\"");
			map.put(nn[0], nn[1]);
		}
		return map;
	}

	public static void main(String args[]) {
		Map<String, String> map = new HashMap();
		map.put("name", "12342");
		map.put("pass", "4355");
		map.put("jos", "999");
		map.put("wang", "999");
		JsonType jsonType = new JsonType();
		String mm = jsonType.simpleMapToJsonStr(map);
		System.out.println(mm);
		Map mp = jsonType.getData(mm);
		System.out.println(mp.get("wang"));

	}

}